import {Body, Controller, Get, HttpStatus, Post, Query, Res} from '@nestjs/common';
import {Response} from 'express';
import {CommonRedisService} from '../../service/common.redis.service';
import {CommonResponseDto} from '../../dto/common.response.dto';
import {ResCode} from '../../common/constants';
import AppConfig from 'config/app.config';
import { uuid } from '../../common/utils';

@Controller('apply-admin/login')
export class LoginController {
  constructor(private readonly commonRedisService: CommonRedisService) {}

  @Post('/login')
  async login(@Body() params: { username: string, password: string }, @Res({ passthrough: true }) res: Response) {
    console.log(params)
    res.status(HttpStatus.OK);
    params.username = params.username || '';
    params.password = params.password || '';
    // Ptdx@123
    // 75368a5aed8c40763cde82c8a2afe6a0
    const { username, password } = params;
    if (username && password && username === 'admin' && password === '75368a5aed8c40763cde82c8a2afe6a0') {
      try {
        const token = uuid(64)
        await this.commonRedisService.set(`${AppConfig.DingDing.AdminAccessTokenKey}`, token, 2592000)// 30天过期
        return new CommonResponseDto(ResCode.OK, 'success', {token});
      } catch(e) {
        console.log(e)
        return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
      }
    }
    return new CommonResponseDto(ResCode.ERROR, '用户名或密码错误', null);
  }
}
